#!/bin/zsh

# 解压到指定位置
extract2path(){
  echo "[Debug] === extract2path()  ==="
  target_file_path=$1
  echo "[Debug] target_file_path = $target_file_path"
  extract_file_path=$2
  echo "[Debug] extract_file_path = $extract_file_path"
  move_file_path=$3
  echo "[Debug] move_file_path = $move_file_path"
  download_file_save_path=$4
  echo "[Debug] download_file_save_path = $download_file_save_path"
  # 判断参数是否为空
  if [ ! -n "$4" ]; then
    download_file_save_path="$HOME/Downloads"
  else
    echo "[INFO] 参数不为空!"
    download_file_save_path=$4
  fi
  echo "[Debug] download_file_save_path = $download_file_save_path"

  # 判断是什么文件，使用不同解压命令解压
  ext_name=`echo $target_file_path | awk -F'/' '{print $NF}' | awk -F'.' '{print $NF}' `
  echo "[Debug] ext_name = $ext_name"
  # 解压
  case $ext_name in
    'zip')   
        echo "[Debug] $target_file_path 是 *.zip"
        # unzip -d ~/.local/bin ./ffmpeg-113507-gf6b7b473d4.zip
        # unzip -d "$HOME/Downloads"  "$target_file_path"
        unzip -d "$download_file_save_path"  "$target_file_path"

        # unzip -v  -d "$download_file_save_path"  "$target_file_path"  | grep 'Stored' |awk '{print $8}' |sed 's!/!!' # 获取解压后的列表
        # [shell获取unzip解压后的文件夹名_unzip 解压文件名获取-CSDN博客](https://blog.csdn.net/weixin_43935187/article/details/115178270)
    ;;
    '.tar.gz' | 'gz' | ) 
        echo "[Debug] $target_file_path 是*.tar.gz"
        # 解压
        # tar zxf $target_file_path -C "$HOME/Downloads"
        tar zxf $target_file_path -C "$download_file_save_path"
    ;;
    *) 
      echo "[Debug] $target_file_path 是未知格式不知道如何解压"
    ;;
  esac
  # 解压后 移动到指定位置
  # cd "$HOME/Downloads"
  # cd "$download_file_save_path"
  # pwd
  # ls
  cp $move_file_path $extract_file_path
  # cd -
}

# 下载
file_downloader(){
  echo "[Debug] === file_downloader()  ==="
  file_download_url=$1
  echo "[Debug] file_download_url = $file_download_url"
  # 判断参数是否为空
  if [ ! -n "$2" ]; then
    echo "[Debug] 参数为空!"
    download_file_save_path="$HOME/Downloads"
    file_name=`echo $file_download_url | awk -F'/' '{print $NF}'`
    # file_name=`echo $file_download_url | rev | cut -d '/' -f 1 | rev`
    echo "[Debug] file_name = $file_name"
    save_file_path="$HOME/Downloads/$file_name"
    save_file_path="$2/$file_name"
  else
    echo "[Debug] 参数不为空!"
    save_file_path=$2
  fi
  echo "[Debug] save_file_path = $save_file_path"

  # 判断是否文件存在
  if [[ ! -f "$save_file_path" ]]; then
    echo "[Debug] 文件 $save_file_path 不存在！现在开始下载..."
    if [[  -x "$(which axel)" ]]; then
      echo "[Debug] 使用axel下载 $file_download_url"
      axel --output "$save_file_path" "$file_download_url"
    elif [[  -x "$(which wget)" ]]; then 
      echo "[Debug] 使用wget下载 $file_download_url"
      # wget https://evermeet.cx/ffmpeg/ffmpeg-113507-gf6b7b473d4.zip 
      # wget -c "$file_download_url"
      wget -c -O  "$save_file_path" "$file_download_url"
      # wget -b -c "$file_download_url" # 后台下载+断点续传
    else
      echo "[Debug] 使用curl下载 $file_download_url"
      # curl -C -# -R -O -I "$file_download_url" # 显示下载进度、断点下载
      curl -C -# -R  -I -o "$save_file_path" "$file_download_url" 
      # -O 将文件下载到当前目录下
      # -o 将文件 下载到 download 目录下，并重命名为 test.txt
      # -# 显示下载进度
      # -C 断点续传
      # [Linux 中三个下载工具-wget curl axel - 知乎](https://zhuanlan.zhihu.com/p/451426230)
    fi
  else 
    echo "[INFO] 文件 $save_file_path 存在！"
  fi
}

# 
binary_downloader_install(){
    echo "[Debug] === binary_downloader_install()  ==="

    software_name=$1
    echo "[Debug] software_name = $software_name"
    target_file_download_url=$2
    echo "[Debug] target_file_download_url = $target_file_download_url"
    move_file_path=$3
    echo "[Debug] move_file_path = $move_file_path"
    download_file_save_path=$4
    echo "[Debug] download_file_save_path = $download_file_save_path"
    binary_export_path=$5
    echo "[Debug] binary_export_path = $binary_export_path"

    if [[ ! -x $(which "$software_name") ]]; then
      echo "[Debug] which  = $software_name"
      file_path="$binary_export_path/$software_name"
      if [[ ! -f "$file_path" ]]; then
          if [[ ! -d $binary_export_path ]]; then
            mkdir $binary_export_path
          else
            echo "[INFO] $binary_export_path 已经存在."
          fi
          file_name=`echo $target_file_download_url | awk -F'/' '{print $NF}'`
          # file_name=`echo $file_download_url | rev | cut -d '/' -f 1 | rev`
          file_save_path="$download_file_save_path/$file_name"
          echo "[Debug] file_save_path  = $file_save_path"
          file_downloader $target_file_download_url $file_save_path

          # extract_path="$HOME/.local/bin"
          extract_path=$binary_export_path
          echo "[Debug] extract_path  = $extract_path"
          # 解压到指定位置并赋予执行权限
          # extract2path $file_save_path $extract_path "./fzf"
          extract2path $file_save_path $extract_path $move_file_path
          chmod +x "$extract_path/$software_name" # 赋予执行权限
          # rm -f "$download_file_save_path/$software_name" $file_save_path # 删除过程文件
          rm -f "$download_file_save_path/$software_name" "$download_file_save_path/$move_file_path" # 删除过程文件
      else
        echo "[INFO] $file_path 已经存在."
      fi
    else
      echo "[INFO] $software_name 已经存在."
    fi
}

# 二进制
binary_downloader_install_main(){
    echo "[Debug] === binary_downloader_install_main()  ==="

    software_name=$1
    echo "[Debug] software_name = $software_name"
    target_file_download_url=$2
    echo "[Debug] target_file_download_url = $target_file_download_url"
    # 判断参数是否为空
    if [ ! -n "$3" ]; then
      echo "[Debug] 参数为空!"
      move_file_path="./$software_name"
    else
      echo "[Debug] 参数不为空!"
      move_file_path=$3
    fi
    echo "[Debug] move_file_path = $move_file_path"

    github_proxy="https://gh.api.99988866.xyz/"
    # github_proxy="https://github.moeyy.xyz/"
    download_file_save_path="$HOME/Downloads"
    binary_export_path="$HOME/.local/bin"
    # binary_export_path="/usr/local/bin"
    # binary_export_path="$HOME/Documents/bin"

    # 如果是github 加代理
    domain_name=`echo $target_file_download_url | awk -F'/' '{print $3}'`
    echo "[Debug] domain_name=$domain_name"
    if [ "github.com" = "$domain_name" ] ; then
      echo "[Debug] $target_file_download_url 下载链接是 github.com"
      target_file_download_url="$github_proxy$target_file_download_url"
    else
      echo "[Debug] $target_file_download_url 下载链接不是 github.com 是 $domain_name"
    fi
    echo "[Debug] === $target_file_download_url"

    # 
    binary_downloader_install $software_name $target_file_download_url $move_file_path $download_file_save_path $binary_export_path
}


# binary_downloader_install_main "ffmpeg"      "https://evermeet.cx/ffmpeg/ffmpeg-115189-g0664cbd732.zip"  './ffmpeg'  # ffmpeg   的 macOS 的二进制可执行文件
# binary_downloader_install_main "ffprobe"     "https://evermeet.cx/ffmpeg/ffprobe-115189-g0664cbd732.zip" './ffprobe'  # ffprobe  的 macOS 的二进制可执行文件
# binary_downloader_install_main "ffplay"      "https://evermeet.cx/ffmpeg/ffplay-113169-ge1c1dc8347.zip"  './ffplay'  # ffplay   的 macOS 的二进制可执行文件
# binary_downloader_install_main "ffserver"    "https://evermeet.cx/ffmpeg/ffserver-89723-g2ca65fc7b7.zip" './ffserver'  # ffserver 的 macOS 的二进制可执行文件

binary_downloader_install_main "N_m3u8DL-RE" "https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.2.0-beta/N_m3u8DL-RE_Beta_osx-x64_20230628.tar.gz" './N_m3u8DL-RE_Beta_osx-x64/N_m3u8DL-RE' # m3u8 视频链接下载ts合并 的 macOS 的二进制可执行文件

binary_downloader_install_main "BBDown"      "https://github.com/nilaoda/BBDown/releases/download/1.6.2/BBDown_1.6.2_20240512_osx-x64.zip" './BBDown' # B站视频下载

binary_downloader_install_main "fzf"         "https://github.com/junegunn/fzf/releases/download/0.46.1/fzf-0.46.1-darwin_amd64.zip" './fzf'
binary_downloader_install_main "nap"         "https://github.com/maaslalani/nap/releases/download/v0.1.1/nap_0.1.1_darwin_amd64.tar.gz" "./nap"


# 测试用例
# binary_downloader_install_main "BBDown2"      "https://github.com/nilaoda/BBDown/releases/download/1.6.2/BBDown_1.6.2_20240512_osx-x64.zip" './BBDown' # B站视频下载
# binary_downloader_install_main "N_m3u8DL-RE2" "https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.2.0-beta/N_m3u8DL-RE_Beta_osx-x64_20230628.tar.gz" './N_m3u8DL-RE_Beta_osx-x64/N_m3u8DL-RE' # m3u8 视频链接下载ts合并 的 macOS 的二进制可执行文件
